<template>
  <div class="upload-body">
    <el-dialog v-model="centerDialogVisible" title="上传附件" width="30%" center>
      <div class="upload-title">上传文件</div>
      <upload
        :multipleProgress="true"
        type="doc,xls,ppt,pdf,docx,xlsx,pptx,zip,rar,png,jpg,jpeg"
        @sucess="uploadSucess"
        @fileQueued="uploadFileQueued"
        @progressChange="progressChange"
        :checkReturn="fileList.length>=maxFile"
        v-show="fileList.length<maxFile"
        fileName="community"
        :returnFile="true"
      >
        <div class="upload-box f-d-c f-j-a-c" :class="{'upload-box-disabled':fileList.length>=maxFile}">
          <svg-icon class="svg-icon" name="icon-wj"></svg-icon>
          <span class="upload-box-title">选择文件</span>
          <span class="upload-box-text"
            >可直接将文件拖拽到此处进行上传，支持格式：doc, xls, ppt, pdf, docx, xlsx, pptx, zip, rar, png, jpg, jpeg, 最多上传{{maxFile}}个文件</span
          >
        </div>
      </upload>

      <div class="file-list">
        <div class="file-item f-j-sb-a-c" v-for="(item, index) in fileList" :key="index">
          <svg-icon class="svg-icon-file-type" name="icon-zip"></svg-icon>
          <div class="file-info">
            <div class="file-name">{{ item.fileName }}</div>
            <div class="file-size" v-if="!item.isUpload">
              {{ toFixedNum(item.file.size / 1024 / 1024, 2) }}MB
            </div>
            <div class="file-upload-progress" v-else>
              <div
                class="file-upload-progress-inner"
                :style="{ width: item.progress + '%' }"
              ></div>
            </div>
          </div>
          <svg-icon
            class="svg-icon-del"
            name="icon-upload-shanchu"
            v-if="!item.isUpload"
            @click="fileDel(item, index)"
          ></svg-icon>
        </div>
      </div>

      <template #footer right>
        <span class="dialog-footer flex">
          <div class="close-btn f-j-a-c" @click="close">取消</div>
          <div class="confirm-btn f-j-a-c" @click="confirm">确定</div>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { toFixedNum } from "@/utils/common.js";
import upload from "@/components/upload.vue";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {
  defineComponent,
  reactive,
  toRefs,
  onUnmounted,
  onMounted,
  computed,
  getCurrentInstance,
} from "vue";
import { ElMain, ElMessage } from "element-plus";
export default defineComponent({
  components: {
    upload,
  },
  props:{
    uploadedList:{
      type:Array,
      default:()=>[]
    }
  },
  setup(props, { attrs, slots, emit }) {
    const router = useRouter();
    const { proxy } = getCurrentInstance();
    const store = useStore();
    let data = reactive({
      centerDialogVisible: false,
      fileList: [], //文件列表
    });

    // 全局配置
    let maxFile = computed(()=>{
      let config = store.state.globalConfig;
      if(config.page && config.page.create){
        return config.page.create.max_file;
      }else{
        return 1;
      }
    })

    // 文件上传成功
    let uploadSucess = (obj) => {
      data.fileList.forEach((item) => {
        if (item.file.name == obj.name) {
          item.isUpload = false;
          item.url = obj.url;
        }
      });
    };

    // 文件上传前
    let uploadFileQueued = (file) => {
      let obj = {
        file: file,
        isUpload: true,
        progress: 0,
        fileName:file.name.slice(13)
      };
      data.fileList.push(obj);
    };

    // 文件进度条
    let progressChange = (stepObj) => {
      let fileName = stepObj.file.name;
      data.fileList.forEach((item) => {
        if (item.file.name == fileName) {
          item.progress = stepObj.progress;
        }
      });
    };

    // 文件删除
    let fileDel = (itme, index) => {
      data.fileList.splice(index, 1);
    };

    // 打开弹框
    let open = () => {
      if(props.uploadedList.length) {
        data.fileList = props.uploadedList.map(item=>{
          return {
            isUpload:false,
            progress:100,
            fileName:item.name,
            url:item.url,
            file:{
              size:item.size
            }
          }
        })
      };
      data.centerDialogVisible = true;
    };

    // 取消
    let close = () => {
      data.fileList = [];
      data.centerDialogVisible = false;
    };

    // 确定
    let confirm = () => {
      if(!data.fileList.length) return ElMessage.warning("请上传附件");
      let filterList = data.fileList.filter(item=>item.isUpload);
      if(filterList.length) return ElMessage.warning("请等待文件传输完成");
      let file = data.fileList.map(item=>{
        return {
          name:item.fileName,
          url:item.url,
          size:item.file.size
        }
      })
      emit("confirm",file);
      close();
    };

    return {
      maxFile,
      fileDel,
      toFixedNum,
      uploadFileQueued,
      uploadSucess,
      progressChange,
      close,
      confirm,
      open,
      ...toRefs(data),
    };
  },
});
</script>
<style lang="scss" scoped>
.upload-body {
  :deep(.el-dialog.el-dialog--center) {
    border-radius: 20px;
    min-width: 520px;
    max-width: 600px;
  }

  :deep(.el-dialog--center .el-dialog__footer) {
    text-align: right !important;
  }
}

.upload-title {
  font-size: 14px;
  color: #000000;
  font-weight: bold;
}

.upload-box-disabled{
  cursor: not-allowed;
}

.upload-box {
  border-radius: 10px;
  border: 1px dashed #ececec;
  padding: 35px 80px;
  line-height: 20px;
  margin-top: 10px;

  .svg-icon {
    width: 24px;
    height: 24px;
  }
  .upload-box-title {
    color: #64c8dc;
    font-size: 12px;
  }
  .upload-box-text {
    padding-top: 6px;
    font-size: 12px;
    color: #656f79;
  }
}

// 文件列表
.file-list {
  max-height: 270px;
  overflow-y: auto;
  margin-top: 10px;
  .file-item {
    border-radius: 10px;
    background: #f3f4f5;
    padding: 17px 20px;
    margin-top: 10px;
    .svg-icon-file-type {
      width: 32px;
      height: 32px;
      margin-right: 12px;
    }
    .file-info {
      flex: 1;
      .file-name {
        color: #000000;
        font-size: 14px;
        padding-bottom: 6px;
      }
      .file-size {
        color: #656f79;
        font-size: 12px;
        line-height: 20px;
      }
      .file-upload-progress {
        width: 100%;
        height: 6px;
        border-radius: 4px;
        background: #eaedf0;
        position: relative;
        margin-top: 8px;
        .file-upload-progress-inner {
          left: 0;
          top: 0;
          height: 6px;
          position: absolute;
          border-radius: 4px;
          background: linear-gradient(90deg, #9ed8e6 0%, #a1f0c0 100%);
        }
      }
    }
    .svg-icon-del {
      width: 24px;
      height: 24px;
      cursor: pointer;
      margin-left: 10px;
    }
  }
}

// 底部按钮
.dialog-footer {
  font-size: 14px;
  font-weight: bold;
  justify-content: flex-end;
  > div {
    height: 36px;
    width: 80px;
    border-radius: 10px;
    margin-left: 10px;
    cursor: pointer;
    background: #f3f4f5;
    color: #656f79;
    &:active {
      opacity: 0.7;
    }
  }
  .confirm-btn {
    background: linear-gradient(90deg, #9ed8e6 0%, #a1f0c0 100%);
    color: #ffffff;
  }
}
</style>
